#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "model.h"
#include "controller_bstPhuc.h"
#define  FLUSH while(getchar() != '\n')


#define MAX_STR 100

/* Read the file */
/* Pre   none*/
/* Post   none*/
/* Return  pointer to file*/

FILE* readFile(char str[])
{
// Declarations
	char filename[MAX_STR];
	FILE* infile;
	int result;
	char text[]=".txt";
	char *ps;

// Statements
	do {
		printf("Enter the filename: ");
		scanf("%[^\n]", filename);
		FLUSH;

		ps=strrchr(filename,'.'); //altered to detect if user writes a valid filename, append .txt --Faith
		if(ps!=NULL){

			if(strcmp(ps,text)==0){
				infile = fopen(filename, str);
				if(!infile)
					result=0;
				else
					result=1;
			}else
				result=0;
				
		}else
			result=0;

	} while(!result);
	
	return infile;
}// readFile 

/* Store the data to SOLDIER struct  */
/* Pre   an input string after read one line*/
/* Post   data has been stored*/
/* Return pointer to SOLDIER*/
SOLDIER* storeToSOLFcn(char str[])
{
   //Declarations
   char* pc;
   int exp;
   SOLDIER* data = NULL;


   //Statements
    // store into data 
    data = (SOLDIER*) malloc (sizeof(SOLDIER));
    if(!data)
      exit(101);

	// store name
    pc = strtok(str, ",");
	data->name = (char*) malloc (strlen(pc)+1);
	if (!(data->name))
      exit(101);
	strcpy(data->name, pc);
    
	// store experience
	pc = strtok(NULL, ",");
	sscanf(pc,"%d",&exp);
	data->experience = exp;

	// store rank
	pc = strtok(NULL, ",");
    data->rank = (char*) malloc (strlen(pc)+1);
    if (!(data->rank))
      exit(101);
    strcpy(data->rank, pc);

	// store division
	pc = strtok(NULL, ",");
	data->division = (char*) malloc (strlen(pc)+1);
    if (!(data->division))
      exit(101);
    strcpy(data->division, pc);

	// store jobCat
	pc = strtok(NULL, ",");
	pc[strlen(pc) - 1] = '\0';
	data->jobCat = (char*) malloc (strlen(pc)+1);
	if (!(data->jobCat))
      exit(101);
	strcpy(data->jobCat, pc);

	return data;

}// storeSOLDIERFcn


/* Store the data into BST */
/* Pre   bst has been created*/
/* Post   data has been stored*/
/* Return  none*/
void storeToBSTFcn(BST* q, char str[])
{
	//Declarations
	SOLDIER* data;

	//Statements
	data = storeToSOLFcn(str);
	insertBST(q, data);

}// storeToBSTFcn


/* Print the tree*/
/* Pre bst has been created*/
/* Post datas print */
/* return none */
void printFcn(BST * bst, void (*process) (void * dataPtr) )
{
  // Statements
  if (emptyBST(bst))
    printf(" There is no task to print !");
  else
    inOrder(bst,process);

}// printFcn

/* Process function */
/* Pre pointer to data*/
/* Post data been process*/
/* Return none*/
void process (void * dataIn)
{
	// Declarations
	SOLDIER * data;

	// Statements
	data = (SOLDIER * ) dataIn;
	printf("        Name: %s,\n  Experience: %d,\n        Rank: %s,\n    Division: %s,\nJob Category: %s\n\n",
		data->name,data->experience,data->rank,data->division,data->jobCat);
}// process

/*	========freeMem============
	Free memory of the data 
	Pre	Data pointer
	Post Data freed
*/
void freeMem (void * dataPtr)
{
	//Local Definitions
	SOLDIER* data;

	//Statements
	data = (SOLDIER*)dataPtr;

	free(data->name);
	free(data->rank);
	free(data->division);
	free(data->jobCat);
	free(data);

}// freemem

/*	=========printKey===========
	Use to print indented key
	Pre node and level
	Post key printed
*/
void printKey (void * dataPtr, int level)
{
	//Local Definitions
	int i;
	SOLDIER* data;

	//Statements
	
	data = (SOLDIER*)dataPtr;
	for(i = 0; i < level; i++)
	{
		printf("     ");
	}
	
	printf("%d %s\n", level, data->name);

}// printKey


/* PrintBlank*/
/* Pre level of tree to print*/
/* Post blank node get printed in indented tree*/
/* Return none*/
void printBlank(int level)
{
	// Declarations
	int i;

	//Statment
	for(i = 0; i < level; i++)
	{
		printf("     ");
	}
	printf("%d NULL\n", level);
}// printBlank


/* compare function */
/* Pre   pointer to char*/
/* Post   none*/
/* Return  0 equal,1 greater, -1 less*/
int compare(void* d1, void* d2)
{
	//Declarations
	SOLDIER* c1;
	SOLDIER* c2;
	int  i;
	int  l;
	char tempStr1[MAX_STR];
	char tempStr2[MAX_STR];


	c1 = (SOLDIER*)d1;
	c2 = (SOLDIER*)d2;
	strcpy(tempStr1, c1->name);
	strcpy(tempStr2, c2->name);
	l = strlen(tempStr1);
	for(i = 0; i < l; i++)
		tempStr1[i] = toupper(tempStr1[i]);
	l = strlen(tempStr2);
	for(i = 0; i < l; i++)
		tempStr2[i] = toupper(tempStr2[i]);
	
	
	if (strcmp(tempStr1, tempStr2) > 0)
		return 1;
	else if (strcmp(tempStr1, tempStr2) < 0)
		return -1;
	else 
		return 0;
}// compare


/* Print to file*/
/* Pre bst has been stored*/
/* Post print bst, and indented tree to file*/
/* Return none*/
void printToFile (BST* bst)
{
	// Local Definitions
	FILE* outfile;
	// SOLDIER* data;


	// Statements
	do {
		outfile = readFile("w");
	}
	while (!outfile);
	
	inOrderForFile(bst, processToFile, outfile);

	fclose(outfile);
	
	return;
}// printToFile

/* Process wrting to file*/
/* Pre bst has been stored*/
/*     outfile is the output file pointer*/
/* Post print bst with key order to a file*/
/* Return none*/
void processToFile(void * dataPtr, FILE *outfile)
{
	SOLDIER *data;
	
	//Statements
	data = (SOLDIER *) dataPtr;
	
	fprintf(outfile,"%s,%d,%s,%s,%s\n",
		data->name,data->experience,data->rank,data->division,data->jobCat);
	
	return;
}
